import pygtk
pygtk.require('2.0')
import gtk
# import os, sy
import os, sys, string

from yadict.gui.modaldialog import ModalDialog
from yadict.util.gtkutil import getScrolledWindow
from yadict.util.sqlutil import searchLikeTerms, getTranslate

class FindPage(gtk.VBox):
  
    def clear(self, widget):
        self.entryTerm.set_text("")
        
    def checkInsert(self,widget):
        return self._checkInsert(False)

    def _checkInsert(self, force):

        # is the completion active?
        if force == False and self.checkCompletion.get_active() == False:
            # clear listore          
            return

        if self.yadictData.connection == None:
            modal = ModalDialog("No database loaded")
            return
        
        self.liststore.clear()

        #print self.entryTerm.get_text()

        terms = searchLikeTerms(self.yadictData.connection, self.entryTerm.get_text(), self.yadictData.limitCompletion) 
        
        if self.entryTerm.get_text() == None or self.entryTerm.get_text().strip() == "":
            return
        
        for term in terms:
            # we have to lowerize the term for predective entry
            self.liststore.append([term[0].lower()])

    def complete(self, widget):
        self.entryTerm.grab_focus()
        self.entryTerm.set_position(len(self.entryTerm.get_text()))
        self._checkInsert(True)
        
        
    def find(self, widget):
        
        if self.yadictData.connection == None:
            modal = ModalDialog("No database loaded")
            return
        
        term = getTranslate(self.yadictData.connection, self.entryTerm.get_text())
        if term == None:
            self.textbufferTranslate.set_text("No Values")
        else:
            self.textbufferTranslate.set_text(term)
        
    def match_cb(self, completion, model, iter):
        # get translate
        print model[iter][0], 'was selected'
        
        term = getTranslate(self.yadictData.connection, model[iter][0])
        self.textbufferTranslate.set_text(term)

    def activate_cb(self, entry):
        text = entry.get_text()
        print text
        #if text:
        #    if text not in [row[0] for row in self.liststore]:
        #        self.liststore.append([text])
        #        entry.set_text('')
        return
    
    def buildLayout(self):

        # begin term
        hTermBox = gtk.HBox(False,5)
        hTermBox.show()

        labelTerm = gtk.Label("Term:")
        labelTerm.show()

        self.entryTerm = gtk.Entry()
        self.entryTerm.show()
        
        # prepare entry completition
        self.completion = gtk.EntryCompletion()
        self.liststore = gtk.ListStore(str)
        self.completion.set_model(self.liststore)
        self.entryTerm.set_completion(self.completion)
        self.completion.set_text_column(0)
        self.completion.connect('match-selected', self.match_cb)
# we have to verify this method 
        #self.entryTerm.connect('activate', self.activate_cb)
        self.entryTerm.connect("changed", self.checkInsert)
        
        hTermBox.pack_start(labelTerm, False, False, 5)
        hTermBox.pack_start(self.entryTerm, True, True, 5)
        # end term
        
        # begin options
        
        hOptionsBox = gtk.HBox(False,5)
        hOptionsBox.show()
              
        self.checkCompletion = gtk.CheckButton(label="Completion")
        self.checkCompletion.show()
        
        btFind = gtk.Button("Find")
        btFind.connect("clicked", self.find)
        btFind.show()
       
        btComplete = gtk.Button("Complete")
        btComplete.connect("clicked", self.complete)
        #btComplete.show()

        btClear = gtk.Button("Clear")
        btClear.connect("clicked", self.clear)
        btClear.show()
        
        hOptionsBox.pack_start(self.checkCompletion, True, True, 5)
        hOptionsBox.pack_start(btFind, True, True, 5)
        #hOptionsBox.pack_start(btComplete, True, True, 5)
        hOptionsBox.pack_start(btClear, True, True, 5)
        
        # end options
                
        # begin translate

        textviewTranslate = gtk.TextView()
        textviewTranslate.show()
        textviewTranslate.set_wrap_mode(True)
        textviewTranslate.set_sensitive(False)
        self.textbufferTranslate = textviewTranslate.get_buffer()
        self.textbufferTranslate.set_text("")
        
        # prepare scrolled widget
        scrolledWidget = getScrolledWindow()
        scrolledWidget.add_with_viewport(textviewTranslate) # if we use a non scrollable widget we need add_with_viewport
        scrolledWidget.show()
        
        # end translate
        
        self.pack_start(hTermBox, False, False, 10)
        self.pack_start(hOptionsBox, False, False, 10)
        self.pack_start(scrolledWidget, True, True, 10)

    def __init__(self, yadictData):
        
        # something like super() in java
        gtk.VBox.__init__(self, False, 5)
        
        self.yadictData = yadictData
        self.buildLayout()
        
        # show me
        self.show()